package cn.edu.xust.util;

import cn.hutool.crypto.digest.DigestUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;


@Slf4j
public class ValidUtils {




    public static String sign(String appId, String secret, String rand, String timestamp) {
        String[] items = {appId, rand, secret, timestamp};
        Arrays.sort(items);
        String stringToHash = String.join("", items);
        String signature = DigestUtil.sha1Hex(stringToHash);
        return signature;
    }


    public static boolean signvertigy(String appId, String secret, String rand, String timestamp,String sign) {
        String now = String.valueOf(System.currentTimeMillis()).substring(0, 10);
        String[] items = {appId, rand, secret, timestamp};
        Arrays.sort(items);
        String stringToHash = String.join("", items);
        String signature = DigestUtil.sha1Hex(stringToHash);
        boolean time = Long.parseLong(now) - Long.parseLong(timestamp) < 60 * 10;
        boolean equals = signature.equals(sign);
        if(!equals){
            log.info("签名错误:right:{}:real:{}",signature,sign);
        }
        if(!time){
            log.info("校验时间超时:now:{}:real:{}",now,timestamp);
        }
        return equals && time;
    }

    public static void main(String[] args) {
        System.out.println(sign("ryZODkXZpLTTCjmeXPXWz","MnhiFWQlWaCbEhnDBEFMgRhDHdhywaTgi","123456","1740975146"));
    }

}

